<?php

namespace app\home\admin;
use app\admin\controller\Admin;
use app\home\model\User;
use think\Loader;
use think\Request;

class Redbeans extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function costlist(Request $request)
    {
//        根据角色来获取对应的用户列表
        $data = $request->param();

        //是否设置了关键字
        if (isset($data['cost_type']) && !empty($data['cost_type']) && $data['cost_type'] != 6){
            $where['C.cost_type'] = $data['cost_type'];
        }else{
            $where['C.cost_type'] = ['in',[1,2,3,4,5]];
        }
        //设置是否筛选
        if (!empty($data['create_time'])){
            $time = explode('~',$data['create_time']);
            $start = strtotime($time[0]);
            $end = strtotime($time[1]);
            $where['C.create_time'] = ['between',[$start,$end]];
        }
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        $res = db('home_coinrecord')
            ->alias("C")
            ->join('home_users F','C.uid = F.id')
            ->where($where)
            ->field('C.id,C.create_time,C.toid,F.nickname fromname,C.giftid,C.sqid,C.topid,C.tapid,C.num,C.rewardnum,C.cost_type,C.status')
            ->order('create_time desc')
            ->paginate(12)->each(function ($item){
                if (empty($item['toid'])){
                    $item['toname'] = '无';
                }else{
                    $item['toname'] = db('home_users')->where('id',$item['toid'])->value('nickname');
                }
                if ($item['cost_type'] == 1){
                    $item['contents'] = db('home_squares')->where('id',$item['sqid'])->value('contents');
                    $item['cost_type'] = '婚恋置顶';
                    $item['tapname'] = '无';
                    $item['topname'] = db('home_tops')->where('id',$item['topid'])->value('name');
                    $item['giftname'] = '无';
                }elseif($item['cost_type'] == 2){
                    $item['contents'] = '无';
                    $item['cost_type'] = '打招呼';
                    $item['tapname'] = '无';
                    $item['topname'] = '无';
                    $item['giftname'] = '无';
                }elseif($item['cost_type'] == 3){
                    $item['contents'] = '无';
                    $item['cost_type'] = '赠送礼物';
                    $item['tapname'] = '无';
                    $item['topname'] = '无';
                    $item['giftname'] = db('home_gifts')->where('id',$item['giftid'])->value('giftname');
                }elseif($item['cost_type'] == 4){
                    $item['contents'] = '无';
                    $item['cost_type'] = '导师周易咨询';
                    $item['tapname'] = db('home_taps')->where('id',$item['tapid'])->value('name');
                    $item['topname'] = '无';
                    $item['giftname'] = '无';
                }elseif($item['cost_type'] == 5){
                    $item['contents'] = '无';
                    $item['cost_type'] = '婚恋置顶';
                    $item['tapname'] = '无';
                    $item['topname'] = db('home_tops')->where('id',$item['topid'])->value('name');
                    $item['giftname'] = '无';
                }
                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
                if ($item['status'] == 2){
                    $item['status'] = '消费成功';
                }else{
                    $item['status'] = '消费失败';
                }
                return $item;
            });
        if ($res){
            $this->assign('costlist',$res);
        }else{
            return $this->success('缺少对应用户数据');
        }
        return $this->fetch();


    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }
           //获取礼物列表
    public function giftlist(Request $request)
    {
        $data = $request->param();
        $where['status'] = ['in',[1,2]];
        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['giftname'] = ['like',"%{$data['keyword']}%"];
        }

        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $listrow = $data['listrow'] :  $listrow = 7;
        $res = db('home_gifts')
            ->where($where)
            ->paginate($listrow)->each(function ($item){
                $item['create_time'] = date("Y-m-d H:i:s",$item['create_time']);
                return $item;
            });
        if ($res){
            $this->assign('giftlist',$res);
        }else{
            return $this->success('缺少对应用户数据');
        }
        return $this->fetch('giftlist');


    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delgift(Request $request)
    {
        $data = $request->param();
        if (isset($data['ids'])){
            $where['id'] = ['in',$data['ids']];
        }
        $res = db('home_gifts')->delete($where);
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    /**
     * 更新是否显示
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function changegiftstatus(Request $request)
    {
        $data = $request->param();
        if (!empty($data['id'])){
            $id = $data['id'];
        }else{
            return $this->error('参数缺失');
        }
        if ($data['val'] == 0){
            $insert['status'] = 2;
        }else{
            $insert['status'] = 1;
        }
        $result = db('home_gifts')
            ->where('id',$id)
            ->update($insert);
        if ($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function updategift(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            $insert['id'] = $data['id'];
            $insert['giftname'] = $data['giftname'];
            $insert['giftpics'] = $data['image'];
            $insert['cost'] = $data['cost'];
            $insert['update_time'] = time();
            $res = \db('home_gifts')->update($insert);
            if ($res){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }
        $id = $data['id'];
        $info = \db('home_gifts')->where('id',$id)->find();
        if ($info){
            $this->assign('list',$info);
            return $this->fetch('addgift');
        }else{
            return $this->error('无数据');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function addgift(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            $insert['giftname'] = $data['giftname'];
            $insert['cost'] = $data['cost'];
            $insert['giftpics'] = $data['image'];
            $insert['create_time'] = time();
            $insert['status'] = 1;
            $res = \db('home_gifts')->insert($insert);
            if ($res){
                return $this->success('新增成功');
            }else{
                return $this->error('添加失败');
            }
        }else{
            return $this->fetch();
        }
    }
    //总共花费的红豆币数量
    public function jjj()
    {
        if (!isset($_FILES['file'])){
            return apistatus(0,'确少file字段');
        }
        $res = files_upload('file','/upload/'.'headicon'.date('Y-m-d',time()));
        if ($res['status']==200){
            $data['path'] = $res['path'][0];
            return apistatus(1,'文件上传成功',$data);
        }
        return apistatus(0,'文件上传失败',$res['error']);
    }


    //获取礼物列表
    public function toplist(Request $request)
    {
        $data = $request->param();
        $where['status'] = ['in',[1,2]];
        //是否设置了关键字
        if (isset($data['keyword']) && !empty($data['keyword'])){
            $where['name'] = ['like',"%{$data['keyword']}%"];
        }

        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $listrow = $data['listrow'] :  $listrow = 7;
        $res = db('home_tops')
            ->where($where)
            ->paginate($listrow)->each(function ($item){
                $item['create_time'] = date("Y-m-d H:i:s",$item['create_time']);
                if (empty($item['update_time'])){
                    $item['update_time'] = '未更新';
                }else{
                    $item['update_time'] = date("Y-m-d H:i:s",$item['update_time']);
                }

                return $item;
            });
        if ($res){
            $this->assign('toplist',$res);
        }else{
            return $this->success('缺少对应用户数据');
        }
        return $this->fetch('toplist');


    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deltop(Request $request)
    {
        $data = $request->param();
        if (isset($data['ids'])){
            $where['id'] = ['in',$data['ids']];
        }
        $res = db('home_tops')->delete($where);
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }

    /**
     * 更新是否显示
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function changetopstatus(Request $request)
    {
        $data = $request->param();
        if (!empty($data['id'])){
            $id = $data['id'];
        }else{
            return $this->error('参数缺失');
        }
        if ($data['val'] == 0){
            $insert['status'] = 2;
        }else{
            $insert['status'] = 1;
        }
        $result = db('home_tops')
            ->where('id',$id)
            ->update($insert);
        if ($result){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function updatetop(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            if (!is_numeric($data['days']) || !is_numeric($data['cost'])){
                return $this->error('置顶天数数据格式错误,请填写数字');
            }
            $insert['id'] = $data['id'];
            $insert['name'] = $data['name'];
            $insert['cost'] = $data['cost'];
            $insert['days'] = $data['days'];
            $insert['update_time'] = time();
            $res = \db('home_tops')->update($insert);
            if ($res){
                return $this->success('更新成功');
            }else{
                return $this->error('更新失败');
            }
        }
        $id = $data['id'];
        $info = \db('home_tops')->where('id',$id)->find();
        if ($info){
            $this->assign('list',$info);
            return $this->fetch('addtop');
        }else{
            return $this->error('无数据');
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function addtop(Request $request)
    {
        //
        $data = $request->param();
        if ($request->isPost()){
            if (!is_numeric($data['days']) || !is_numeric($data['cost'])){
                return $this->error('置顶天数数据格式错误,请填写数字');
            }
            $insert['name'] = $data['name'];
            $insert['cost'] = $data['cost'];
            $insert['days'] = $data['days'];
            $insert['create_time'] = time();
            $insert['status'] = 1;
            $res = \db('home_tops')->insert($insert);
            if ($res){
                return $this->success('新增成功');
            }else{
                return $this->error('添加失败');
            }
        }else{
            return $this->fetch();
        }
    }
}
